/**
 * 1
 *
 * @Author : roryrywang
 * @DateTime : 24-9-7 下午7:34
 */

#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>

using namespace std;

int main() {
    string s;
    int k;
    cin >> s >> k;

    unordered_map<char, int> map;
    map['H'] = 1;
    map['S'] = 2;
    map['D'] = 3;
    map['C'] = 4;

    int sum = 0;
    for (int i = 0; i < k; ++i) {
        sum += map[s[i]];
    }
    int res = sum;
    for (int i = k - 1, j = s.size() - 1; i >= 0; --i, --j) {
        sum = sum - map[s[i]] + map[s[j]];
        res = max(res, sum);
    }

    cout << res;

    return 0;
}